/*	
	This program is free software; you can redistribute it and/or modify 
	it under the terms of the GNU General Public License as published by 
	the Free Software Foundation; either version 2 of the License, or 
	any later version.

    	This program is distributed in the hope that it will be useful, but 
	WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
	General Public License for more details.

    	You should have received a copy of the GNU General Public License 
	along with this program; if not, write to the Free Software 
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
	USA
*/

#include<interrupt.h>
#include<stdio.h>

#define PIC1_COMMAND		0x20
#define PIC1_DATA		0x21
#define PIC2_COMMAND		0xA0
#define PIC2_DATA		0xB0
#define PIC_EOI			0x20
#define PIC_RESET		0x11
unsigned char irq_number[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};

void init_pic8259() {
	long flag;
	save_eflag(flag);
	asm("cli");
	out_port(PIC1_COMMAND,PIC_RESET);
	out_port(PIC2_COMMAND,PIC_RESET);
	out_port(PIC1_DATA,0x20);
	out_port(PIC2_DATA,0x28);
	out_port(PIC1_DATA,0x04);
	out_port(PIC2_DATA,0x02);
	out_port(PIC1_DATA,0x01);
	out_port(PIC2_DATA,0x01);
	int i;
	for(i=0;i<8;i++) {disabilita_irq(i,PIC1_DATA);}
	for(i=0;i<8;i++) {disabilita_irq(i,PIC2_DATA);}
	restore_eflag(flag);
}

void abilita_irq(unsigned char irq_num,unsigned char port_num) {
	char dato;
	dato=in_port(port_num);
	dato=dato & irq_number[irq_num];
	out_port(port_num,dato);
}

void disabilita_irq(unsigned char irq_num,unsigned char port_num) {
	char dato;
	dato=in_port(port_num);
	dato=dato|(1<<irq_num);
	out_port(port_num,dato);
}
